<template>
  <div class="flower-list-container">
  
    
    <!-- 分类筛选栏 -->
    <div class="category-filter">
      <button 
        v-for="category in categories" 
        :key="category.id"
        :class="['category-btn', { active: selectedCategory === category.name }]"
        @click="filterByCategory(category.name)"
      >
        {{ category.name }}
      </button>
      <button 
        :class="['category-btn', { active: selectedCategory === 'all' }]"
        @click="filterByCategory('all')"
      >
        全部
      </button>
    </div>
    
    <!-- 花卉卡片网格 -->
    <div class="flower-grid">
      <div 
        v-for="flower in filteredFlowers" 
        :key="flower.id"
        class="flower-card"
        @click="navigateToDetail(flower.id)"
      >
        <div class="flower-image-container">
          <img 
            :src="flower.image" 
            :alt="flower.name"
            class="flower-image"
            loading="lazy"
          />
          <div class="flower-overlay">
            <span class="flower-category">{{ flower.category }}</span>
          </div>
        </div>
        <div class="flower-info">
          <h3 class="flower-name">{{ flower.name }}</h3>
          <p class="flower-scientific-name">{{ flower.scientificName }}</p>
          <div class="flower-meta">
            <span class="flower-origin">{{ flower.origin }}</span>
            <span class="flower-season">{{ flower.floweringSeason }}</span>
          </div>
          <div class="flower-comments">
            <svg class="comment-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path>
            </svg>
            <span>{{ flower.comments.length }}条评论</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 无数据提示 -->
    <div v-if="filteredFlowers.length === 0" class="empty-state">
      <div class="empty-icon">🌸</div>
      <h3>暂无花卉数据</h3>
      <p>当前分类下没有找到相关花卉</p>
    </div>
  </div>
</template>

<script setup>
/**
 * 花卉列表视图：
 * - 展示花卉列表与分类入口
 * - 作为首页路由组件（与 /flowers 互为别名）
 */
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { getFlowers, getCategories, getFlowersByCategory } from '../services/dataService';

const router = useRouter();
const flowers = ref([]);
const categories = ref([]);
const selectedCategory = ref('all');

// 加载花卉数据
onMounted(() => {
  flowers.value = getFlowers();
  categories.value = getCategories();
});

// 根据分类筛选花卉
const filteredFlowers = computed(() => {
  if (selectedCategory.value === 'all') {
    return flowers.value;
  }
  return getFlowersByCategory(selectedCategory.value);
});

// 切换分类
const filterByCategory = (category) => {
  selectedCategory.value = category;
};

// 跳转到花卉详情页
const navigateToDetail = (id) => {
  router.push(`/flower/${id}`);
};
</script>

<style scoped>
.flower-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 20px 0;
}

.page-title {
  font-size: 36px;
  font-weight: 700;
  color: #333;
  margin-bottom: 10px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.page-subtitle {
  font-size: 18px;
  color: #666;
  margin: 0;
}

.category-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-bottom: 40px;
}

.category-btn {
  padding: 10px 20px;
  border: 2px solid #e1e5e9;
  background-color: white;
  border-radius: 25px;
  font-size: 14px;
  font-weight: 500;
  color: #555;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.category-btn:hover {
  border-color: #667eea;
  color: #667eea;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.category-btn.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-color: #667eea;
}

.flower-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.flower-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.flower-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.flower-image-container {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.flower-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.flower-card:hover .flower-image {
  transform: scale(1.05);
}

.flower-overlay {
  position: absolute;
  top: 15px;
  left: 15px;
  background: rgba(255, 255, 255, 0.9);
  padding: 5px 12px;
  border-radius: 15px;
  font-size: 12px;
  font-weight: 600;
  color: #667eea;
}

.flower-info {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.flower-name {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0 0 5px 0;
}

.flower-scientific-name {
  font-size: 14px;
  font-style: italic;
  color: #888;
  margin: 0 0 15px 0;
}

.flower-meta {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
  font-size: 13px;
  color: #666;
}

.flower-origin::before {
  content: '🌍 ';
}

.flower-season::before {
  content: '🌸 ';
}

.flower-comments {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: #999;
  margin-top: auto;
}

.comment-icon {
  color: #d1d5db;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  background-color: #f9fafb;
  border-radius: 16px;
}

.empty-icon {
  font-size: 60px;
  margin-bottom: 20px;
}

.empty-state h3 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.empty-state p {
  font-size: 16px;
  color: #666;
  margin: 0;
}

@media (max-width: 768px) {
  .flower-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .page-title {
    font-size: 28px;
  }
  
  .page-subtitle {
    font-size: 16px;
  }
  
  .category-filter {
    gap: 8px;
  }
  
  .category-btn {
    padding: 8px 16px;
    font-size: 13px;
  }
}
</style>